﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Content;


namespace Platformer1
{
    class Layer
    {
        public Texture2D[] Textures { get; private set; }
        public float ScrollRate { get; private set; }

        /// <summary>
        /// Représente un layer, c'est à dire un plan situé en avant ou en arrière par rapport au plan principal. 
        /// </summary>
        /// <param name="content">Reference au content Manager</param>
        /// <param name="basePath">Path des textures (par défaut : racine de Content</param>
        /// <param name="scrollRate">Ratio de défilement. Ex : 2 = 2x la vitesse du plan principal</param>
        /// <param name="nbSegments">nombre d'images qui composent la texture</param>
        public Layer(ContentManager content, string basePath, float scrollRate, int nbSegments)
        {
            // Parcours des *nbSegement* images pour former le layer 
            Textures = new Texture2D[i];
            for (int i = 0; i < nbSegments; ++i)
                Textures[i] = content.Load<Texture2D>(basePath + "_" + i);
            ScrollRate = scrollRate;
        }


        public void Draw(SpriteBatch spriteBatch, Vector2 cameraPosition)
        {
            // Assume each segment is the same width.
            int segmentWidth = Textures[0].Width;

            // Calculate which segments to draw and how much to offset them.
            float x = cameraPosition.X * ScrollRate;
            float y = cameraPosition.Y * ScrollRate;
            int leftSegment = (int)Math.Floor(x / segmentWidth);
            int rightSegment = leftSegment + 1;
            x = (x / segmentWidth - leftSegment) * -segmentWidth;

            spriteBatch.Draw(Textures[Math.Abs(leftSegment) % Textures.Length], new Vector2(x,0f), Color.White);
            spriteBatch.Draw(Textures[Math.Abs(rightSegment) % Textures.Length], new Vector2(x + segmentWidth, 0.0f), Color.White);
            //spriteBatch.Draw(Textures[1], new Vector2(x, 0.0f), Color.White);
            //spriteBatch.Draw(Textures[1], new Vector2(x + segmentWidth, 0.0f), Color.White);
        
        }

    }
}
